require "bb_code"

#----------------------------------------------------------------------------#

location = 'D:\Developer\Workspaces\Admin\Bermonruf PHP\posts\MensagensDeErroEFormularios.html'

#----------------------------------------------------------------------------#

location = location.gsub('\\', '/').sub(/([^\/]+)$/, "")
file = $1
Dir.chdir(location)
content = ""
File.open(file, "r") do |efile|
  content = efile.read
end

def make_replace(content, er, replace)
  if replace.kind_of? String
    return content.gsub(er, replace);
  elsif replace.kind_of? Proc
    return content.gsub(er, &replace);
  end
end


#----------------------------------------------------------------------------#

code_divisors = ["[code]", "[/code]"]

head_open = lambda do |matched|
  n = matched.match(/<h(\d)>/)[1]
  "[color=#0000FF][b][size=#{6-n.to_i}]"
end
head_divisors = [head_open, "[/size][/b][/color]\n"]

bold_divisors = ["[b]", "[/b]"]
italic_divisors = ["[i]", "[/i]"]

link_open = lambda do |matched|
  r = matched.match(/<a href="([^"]*)">/)[1]
  "[url=\"#{r}\"]"
end
link_divisors = [link_open, "[/url]"]

paragraph_divisors = ["", "\n"]

list_divisors = {
  :ul => ["[list]", "[/list]", "[*]", ""],
  :ol => ["[list=1]", "[/list]", "[*]", ""],
}

decode_entities = true;

#----------------------------------------------------------------------------#

content.gsub!(/<span style="[^"]+">([^<]*)<\/span>/, "\\1")

content = make_replace(content, /<div style="[^"]+">[^<]*<pre>/, code_divisors[0])
content = make_replace(content, /<\/pre>\s*<\/div>/, code_divisors[1])

content = make_replace(content, /<h(\d)>/, head_divisors[0])
content = make_replace(content, /<\/h(\d)>/, head_divisors[1])

content = make_replace(content, /<b>/, bold_divisors[0])
content = make_replace(content, /<\/b>/, bold_divisors[1])

content = make_replace(content, /<strong>/, bold_divisors[0])
content = make_replace(content, /<\/strong>/, bold_divisors[1])

content = make_replace(content, /<em>/, italic_divisors[0])
content = make_replace(content, /<\/em>/, italic_divisors[1])

content = make_replace(content, /<a href="([^"]*)">/, link_divisors[0])
content = make_replace(content, /<\/a>/, link_divisors[1])

content = make_replace(content, /<p>/, paragraph_divisors[0])
content = make_replace(content, /<\/p>/, paragraph_divisors[1])

content = make_replace(content, /<ul>/, list_divisors[:ul][0])
content = make_replace(content, /<\/ul>/, list_divisors[:ul][1])
content = make_replace(content, /<li>/, list_divisors[:ul][2])
content = make_replace(content, /<\/li>/, list_divisors[:ul][3])

content = make_replace(content, /<ol>/, list_divisors[:ol][0])
content = make_replace(content, /<\/ol>/, list_divisors[:ol][1])
content = make_replace(content, /<li>/, list_divisors[:ol][2])
content = make_replace(content, /<\/li>/, list_divisors[:ol][3])

content = make_replace(content, /&#92;/, "\\")


content = BBCode.decode_entities(content) if decode_entities;

File.open("_"+file, "w") do |efile|
  efile.write(content)
end

#puts content